--[[-------------------------------------------------------------

	Bellah of Stormrage - Jan 2009

	Bese component library to generate default UI components

---------------------------------------------------------------]]

local debug = false

local baseLib, oldMinor = LibStub:NewLibrary("FrameBaseLib", 1)

if not baseLib then
  return	-- already loaded and no upgrade necessary
end

baseLib.embeds = baseLib.embeds or {} -- table containing objects this lib is embedded in.
baseLib.commands = baseLib.commands or {} -- table containing commands registered
baseLib.weakcommands = baseLib.weakcommands or {} -- table containing self, command => func references for weak

function baseLib:SetDebug (value)
	debug = value
	if value then
		print("FrameBaseLib -- debug on")
	else
	    print("FrameBaseLib -- debug off")
	end
end

function baseLib:dbgPrint(text)
	if debug then
		print ("FrameBaseLib - " .. text)
	end
end

-- Get a default frame
-- mandatory: globalName, title, defw, defh
-- optional: parent (defaults to UIParent)
-- optional: border texture (bTex, defaults to no texture)
-- baseLib:GetDefFrame(globalName, title, defW, defH [, parent [, bTex]])
function baseLib:GetDefFrame(globalName, title, defW, defH, ...)
	local parent, bTex = ...
	
	local frame = CreateFrame("Frame", globalName, parent or UIParent)
	frame:ClearAllPoints()
	frame:Hide()
	frame:SetWidth(defW)
	frame:SetHeight(defH)

	if bTex then
		frame:SetBackdrop({
			bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 16,
			edgeFile= bTex, edgeSize = 32,
			insets = {left = 1, right = 1, top = 20, bottom = 1},
		})
	end
	
	frame:SetBackdropColor(24/255, 24/255, 24/255)
	
	frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
	frame:EnableMouse(true)
	frame:RegisterForDrag("LeftButton")
	frame:SetMovable(true)

	frame:SetScript("OnDragStart", function()
			this:StartMoving()
		end)
	frame:SetScript("OnDragStop", function()
			this:StopMovingOrSizing()
		end)

	-- Frame title
	local fTitle = frame:CreateFontString(nil,"OVERLAY")
	fTitle:ClearAllPoints()
	fTitle:SetWidth(defw)
	fTitle:SetHeight(15)
	fTitle:SetPoint("TOPLEFT", frame, "TOPLEFT", 7, -14)
	fTitle:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -28, -14)
	fTitle:SetFont("Fonts\\FRIZQT__.TTF", 12)
	fTitle:SetJustifyH("LEFT")
	fTitle:SetText(title)
	fTitle:SetShadowOffset(.8, -.8)
	fTitle:SetShadowColor(0, 0, 0, 1)
	
	frame.title = fTitle
	
	return frame
end

-- Get a default label
-- mandatory: globalName, parent
-- optional: text (defaults to "")
-- optional: font (defaults to GameFontNormal)
-- GetDefLabel(globalName, parent [, text [, font]])
function baseLib:GetDefLabel(globalName, parent, ...)
	local text, font = ...
	
	local label = parent:CreateFontString(globalName, "OVERLAY", font or "GameFontNormal")
	label:SetTextColor(0.1, 1, 1, 1)
	label:SetText(text or "")
	label:ClearAllPoints()

	return label
end

-- get a default texture
-- mandatory: parent
-- baseLib:GetDefTexture(parent)
function baseLib:GetDefTexture(parent)
	local tex = parent:CreateTexture(nil, "ARTWORK")
	tex:ClearAllPoints()
	tex:SetWidth(parent:GetWidth())
	tex:SetHeight(parent:GetHeight())
	tex:SetPoint("CENTER", parent, "CENTER", 0, 0)
	
	return tex
end

-- get a default single line editbox
-- mandatory: globalName, defW, defH, isEditable
-- optional: text (defaults to "")
-- optional: font (defaults to GameFontNormal)
-- optional: parent (defaults to UIParent)
-- baseLib:GetDefEditbox(globalName, defW, defH, iseEditable [, template [, text [, font [, parent]]]])
function baseLib:GetDefEditbox(globalName, defW, defH, iseEditable, ...)
	local template, text, font, parent = ...
	
	local frame = CreateFrame("EditBox", globalName, parent or UIParent)
	frame:ClearAllPoints()
	frame:SetFontObject(font or "GameFontNormal")
	frame:SetWidth(defW)
	frame:SetHeight(defH)
	frame:SetBackdropColor(0.4, 0,4, 0,4, 0.7)
	frame:SetText(text or "")
	frame:SetAutoFocus(false)
	frame:SetMultiLine(false)
	if not isEditable then
		frame:SetScript("OnEditFocusGained", function() this:ClearFocus() end)
	end
	local tex = getTexture(frame)
	tex:SetTexture(0.3, 0.3, 0.3, 0.7)

	return frame
end

-- Get a default button
-- mandatory: globalName, defW, defH
-- Optional: text (defaults to "")
-- Optional: ID (defaults to no ID))
-- Optional: parent (defaults to UIParent)
-- Optional: template (defaults to "UIPanelButtonTemplate2")
-- Optional: normal Texture / blend mode (nTex, nBlend, defaults to no texture)
-- Optional: Highlight Texture / blend mode (hTex, hBlend, defaults to no texture)
-- baseLib:GetDefButton(globalName, defW, defH [, text [, id [, parent [, template [, nTex, nBlend [, hTex, hBlend]]]]]])
function baseLib:GetDefButton(globalName, defW, defH, ...)
	local text, id, parent, template, nTex, nBlend, hTex, nBlend = ...
	
	local btn = CreateFrame("Button", globalName, parent or UIParent, template or "UIPanelButtonTemplate2")
	btn:SetWidth(defW)
	btn:SetHeight(defH)
	
	if id then
		btn:SetID(id)
	end
	btn:SetText(text or "")
	if nTex then
	    btn:SetNormalTexture(nTex, nBlend)
	end
	if hTex then
	    btn:SetHighlightTexture(nTex, hBlend)
	end

	return btn
end	

-- Get a default Scrollbar
-- mandatory: globalName, defW, defH
-- optional: parent (defaults to UIParent)
-- baseLib:GetDefScrollbar(globalName, defW, defH [, parent])
function baseLib:GetDefScrollbar(globalName, defW, defH, ...)
	local parent = ...
	
	local scrollbar = CreateFrame("Slider", globalName, parent or UIParent, "UIPanelScrollBarTemplate")
	scrollbar:Hide()
	scrollbar:ClearAllPoints()
		
	local tex = scrollbar:CreateTexture(nil,"BACKGROUND")
	tex:SetAllPoints(scrollbar)
	tex:SetTexture(0,0,0,0.4)
	tex:SetMinMaxValues(0,0)
	tex:SetValueStep(1)
	tex:SetWidth(defW)
	tex:SetHeight(defH)
	
	return scrollbar
end

local mixins = {
	"SetDebug", "GetDefFrame","GetDefLabel","GetDefTexture","GetDefEditbox","GetDefButton","GetDefScrollbar"
}

-- Embeds lib into the target object making the functions from the mixins list available on target:..
-- target (object) - target object to embed AceBucket in
function baseLib:Embed( target )
	for k, v in pairs( mixins ) do
		target[v] = self[v]
	end
	self.embeds[target] = true
	return target
end

function baseLib:OnEmbedEnable( target )
	if baseLib.weakcommands[target] then
		for command, func in pairs( baseLib.weakcommands[target] ) do
			target:RegisterChatCommand( command, func, false, true ) -- nonpersisting and silent registry
		end
	end
end

function baseLib:OnEmbedDisable( target )
	if baseLib.weakcommands[target] then
		for command, func in pairs( baseLib.weakcommands[target] ) do
			target:UnregisterChatCommand( command ) -- TODO: this could potentially unregister a command from another application in case of command conflicts. Do we care?
		end
	end
end

for addon in pairs(baseLib.embeds) do
	baseLib:Embed(addon)
end
